package com.jackstraw.create.utils;

/**
 * 所在包：com.jackstraw.create.utils
 * 文件名：
 * 版权：稻草人.
 * 项目：create
 * 功能描述：
 * 修改人：Xinglz
 * 修改时间：2020-03-16 14:51
 *
 * @author jackstraw
 */
public class DbColumnUtil {

    /**
     * 根据数据库字段获取java变量
     *
     * @param column
     * @return
     */
    public static String getFieldByColumn(String column) {
//        if (column.toLowerCase().startsWith("is_")) {
//            column = column.substring(3);
//        }
        column = transUnderlineToCamel(column);
        column = lowerFirstCase(column);
        return column;
    }

    /**
     * 获取类名
     *
     * @param tableName
     * @return
     */
    public static String getClassNameByTableName(String tableName) {
        String className = transUnderlineToCamel(tableName);
        className = upperFirstCase(className);
        return className;
    }

    /**
     * 根据表名获取表的别名
     *
     * @param tableName
     * @return
     */
    public static String getTableAlias(String tableName) {
        String tableAlias = "";
        if (isNotEmty(tableName)) {
            if (tableName.contains("_")) {
                String[] tableNameArr = tableName.split("_");
                for (String tableNamePiece : tableNameArr) {
                    if (isNotEmty(tableNamePiece)) {
                        tableAlias += tableNamePiece.substring(0, 1).toLowerCase();
                    }
                }
            } else {
                tableAlias = tableName.substring(0, 1).toLowerCase();
            }
        } else {
            tableAlias = "t";
        }
        return tableAlias;
    }

    /**
     * 驼峰转下划线（首字母默认小写）
     *
     * @param str
     * @return
     */
    public static String transCamelToUnderline(String str) {
        char[] chars = str.toCharArray();
        int length = chars.length;
        String underlineStr = "";
        for (int i = 0; i < length; i++) {
            char current = chars[i];
            String currentStr = current + "";
            if (i == 0) {
                underlineStr = current + "";
                underlineStr = underlineStr.toLowerCase();
            } else {
                if (current >= 65 && current <= 90) {
                    underlineStr = underlineStr + "_" + currentStr.toLowerCase();
                } else {
                    underlineStr = underlineStr + currentStr;
                }
            }
        }
        return underlineStr;
    }

    /**
     * 将下划线转换成驼峰命名规范（非下划线部分默认全部转小写）
     *
     * @param str
     * @return
     */
    public static String transUnderlineToCamel(String str) {
        if (isNotEmty(str)) {
            String[] strList = str.split("_");
            String camelStr = "";
            int l = strList.length;
            for (int i = 0; i < l; i++) {
                String currentStr = strList[i];
                currentStr = currentStr.trim().toLowerCase();
                if (i > 0) {
                    camelStr = camelStr + upperFirstCase(currentStr);
                } else {
                    camelStr = currentStr;
                }
            }
            return camelStr;
        } else {
            return "";
        }
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String upperFirstCase(String str) {
        if (isNotEmty(str)) {
            String firstLetters = str.substring(0, 1);
            if (str.length() > 1) {
                return firstLetters.toUpperCase() + str.substring(1);
            } else {
                return firstLetters.toUpperCase();
            }
        } else {
            return "";
        }
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     */
    public static String lowerFirstCase(String str) {
        if (isNotEmty(str)) {
            String firstLetters = str.substring(0, 1);
            if (str.length() > 1) {
                return firstLetters.toLowerCase() + str.substring(1);
            } else {
                return firstLetters.toLowerCase();
            }
        } else {
            return "";
        }
    }

    /**
     * 判断字符串是否不为空
     *
     * @param str
     * @return
     */
    public static boolean isNotEmty(String str) {
        if (str == null) {
            return false;
        } else {
            if ("".equals(str.trim())) {
                return false;
            }
            return true;
        }
    }
}
